package com.iespring.service.admin.category;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.category.bo.CategoryBo;
import com.iespring.bean.admin.category.bo.CategoryBoResp;
import com.iespring.bean.admin.category.po.CategoryPo;
import com.iespring.bean.admin.category.vo.CategoryL1Vo;
import com.iespring.bean.admin.category.vo.CategoryVo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.bean.common.CommonData;
import com.iespring.mapper.admin.CategoryBackMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author weigwen
 * @Description:
 * @Date 2022/12/29 17:35
 */

@Service
public class CategoryServiceImpl implements CategoryService {
//    @Value("${static.path}")
//    String staticPath;

    @Autowired
    CategoryBackMapper categoryMapper;

    @Override
    public CommonData categoryList() {
        //selectCategoryByPid(0)即是查询没有父级的CategoryVo对象封装为集合
        List<CategoryVo> categoryVos1 = categoryMapper.selectCategoryByPid(0);
        for (CategoryVo categoryVo1 : categoryVos1) {
            //根据查询到的没有父级的CategoryVo对象的id获取到其子级并封装为集合
            List<CategoryVo> categoryVos2 = categoryMapper.selectCategoryByPid(categoryVo1.getId());
            //将子级集合添加给父级CategoryVo对象
            categoryVo1.setChildren(categoryVos2);
        }
        PageHelper.startPage(1, categoryVos1.size());
        PageInfo<CategoryVo> pageInfo = new PageInfo<>(categoryVos1);
        PageHelper.clearPage();
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData categoryL1() {
        List<CategoryL1Vo> categoryL1Vos = categoryMapper.selectByLevel("L1");
        PageHelper.startPage(1,categoryL1Vos.size());
        PageInfo<CategoryL1Vo> categoryL1VoPageInfo = new PageInfo<>(categoryL1Vos);
        PageHelper.clearPage();
        return CommonData.data(categoryL1VoPageInfo);
    }

    @Override
    public BaseRespVo categoryCreate(CategoryPo categoryPo) {
        categoryMapper.insertCategory(categoryPo);
        CategoryBoResp categoryBoResp = categoryMapper.selectCategoryById(categoryPo.getId());
        return BaseRespVo.ok(categoryBoResp);
    }

    @Override
    public BaseRespVo categoryUpdate(CategoryBo categoryBo) {
        int result = categoryMapper.updateCategory(categoryBo);
        if (result != 1){
            return BaseRespVo.invalidData("更新失败");
        }
        return BaseRespVo.ok();
    }

    @Override
    public BaseRespVo categoryDelete(CategoryBo categoryBo) {
        int result = categoryMapper.updateCategoryDelStatus(categoryBo);
        if (result != 1){
            return BaseRespVo.invalidData("删除失败");
        }
        // TODO
        return BaseRespVo.ok();
    }

}
